﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Com.Toook.Common;
using System.Data.SQLite;

public partial class nst_movie_list : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            InitMovie();
        }
    }
    private void InitMovie()
    {
        string sql = "select id, title, year, area, siteKindId, imgLocal, img " +
            " from " + txtDataTableName.Text.Trim() +
            " where siteKindId='" + txtSiteKindId.Text.Trim() + "' AND (otherUrl is null or otherUrl <> 1) limit 0, 10";//SQLiteDatabase
        SQLiteConnection con = SqliteHelper.GetConnection(txtDatabaseName.Text.Trim());
        SQLiteCommand cmd = new SQLiteCommand(sql, con);
        SQLiteDataReader reader = null;
        try
        {
            con.Open();
            reader = cmd.ExecuteReader();
            GridViewMovie.DataSource = reader;
            GridViewMovie.DataBind();
        }
        catch(Exception ex)
        {
            LogHelper.Log.Error(ex);
        }
        finally
        {
            SqliteHelper.CloseDbResource(con, reader);
        }
    }
    protected void ButtonPublish_Click(object sender, EventArgs e)
    {
        foreach(GridViewRow row in GridViewMovie.Rows)
        {
            CheckBox checkId = (CheckBox)row.FindControl("CheckBoxId");
            if(checkId.Checked)
            {
                //ids += row.Cells[1].Text + ",";
                TransferToPublish(row.Cells[1].Text);
            }
        }
        InitMovie();
    }
    private void TransferToPublish(string id)
    {
        //1、查询数据
        string sql = "select id, title, year, area, siteKindId, actor, content, finalcontent" +
            ", case imgLocal when length(imgLocal) > 1 then imgLocal else img end as img  " +
            " from " + txtDataTableName.Text.Trim() + " where id=" + id;//SQLiteDatabase
        SQLiteConnection con = SqliteHelper.GetConnection(txtDatabaseName.Text.Trim());
        SQLiteCommand cmd = new SQLiteCommand(sql, con);
        SQLiteDataReader reader = null;
       
        string title = "";
        string year = "";
        string area = "";
        string siteKindId = "";
        string actor = "";
        string content = "";
        string finalContent = "";
        string img = "";
        try
        {
            con.Open();
            reader = cmd.ExecuteReader();
            if(reader.Read())
            {
                title = reader["title"].ToString();
                year = reader["year"].ToString();
                area = reader["area"].ToString();
                siteKindId = reader["siteKindId"].ToString();
                actor = reader["actor"].ToString();
                content = reader["content"].ToString();
                finalContent = HttpUtility.UrlDecode(reader["finalContent"].ToString());
                img = reader["img"].ToString();                
            }
        }
        catch(Exception ex)
        {
            LogHelper.Log.Error(ex);
            return;
        }
        finally
        {
            SqliteHelper.CloseDbResource(con, reader);
        }
        //string[] s = { "+++" };
        //string[] play = finalContent.Split(s, StringSplitOptions.RemoveEmptyEntries);
        //string list = "";
        //bool isFirst = true;
        //foreach(string p in play)
        //{
        //    if(isFirst)
        //    {
        //        list = p.Substring(p.IndexOf("qvod://"));
        //        isFirst = false;
        //    }
        //    else
        //    {
        //        list += "#OK#" + p.Substring(p.IndexOf("qvod://")) ;
        //    }            
        //}        
        //2、插入到正式表
        string sqlInsert = "insert into MoviePublish(title, actor, img, url, year, area, type, content, siteKindId) " +
            " values(@title, @actor, @img, @url, @year, @area, @type, @content, @siteKindId)";
        SQLiteConnection conInsert = SqliteHelper.GetConnection("SQLiteDatabase");
        SQLiteCommand cmdInsert = new SQLiteCommand(sqlInsert, conInsert);
        cmdInsert.Parameters.Add("@title", DbType.String, 30).Value = title;
        int y = 2000;
        cmdInsert.Parameters.Add("@year", DbType.Int32).Value = int.TryParse(year, out y) ? y : 2000 ;
        cmdInsert.Parameters.Add("@area", DbType.Int32).Value = GetAreaId(area);
        cmdInsert.Parameters.Add("@siteKindId", DbType.String, 20).Value = siteKindId;
        cmdInsert.Parameters.Add("@actor", DbType.String, 50).Value = actor;
        cmdInsert.Parameters.Add("@content", DbType.String).Value = content;
        cmdInsert.Parameters.Add("@url", DbType.String, 500).Value = finalContent; //list;//数据库中不编码，显示时才编码 HttpUtility.UrlEncode(list);
        cmdInsert.Parameters.Add("@img", DbType.String, 200).Value = img;
        cmdInsert.Parameters.Add("@type", DbType.Int32).Value = ddlMovieType.SelectedValue;
        bool transferOK = true;
        try
        {
            conInsert.Open();
            cmdInsert.ExecuteNonQuery();
        }
        catch(Exception ex)
        {
            transferOK = false;
            LogHelper.Log.Error(ex);
        }
        finally
        {
            SqliteHelper.CloseDbResource(conInsert, reader);
        }

        //3、更新临时表状态
        if (transferOK)
        {
            string sqlUpdate = "update " + txtDataTableName.Text.Trim() + " set otherUrl=1 where id=" + id;
            SQLiteCommand cmdUpdate = new SQLiteCommand(sqlUpdate, con);            
            
            try
            {
                con.Open();
                cmdUpdate.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                LogHelper.Log.Error(ex);
            }
            finally
            {
                SqliteHelper.CloseDbResource(con, null);
            }
        }
    }
    protected void ButtonRefreshData_Click(object sender, EventArgs e)
    {
        InitMovie();
    }
    private int GetAreaId(string area)
    {
        if (string.IsNullOrEmpty(area))
        {
            return 78;//未知
        }
        int id = 77;//其他
        DataRow row = PageData.SelectParamRow("typeName='" + area + "'");
        if (row != null)
        {
            if (!int.TryParse(row["typeId"].ToString(), out id))
            {
                id = 77;
            }
        }
        return id;
    }
}
